package com.core.controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.core.model.BlogModel;
import com.core.validator.BlogValidator;
import com.core.interceptor.BlogInterceptor;

/**
 * BlogController
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
@Before(BlogInterceptor.class)
public class BlogController extends BaseController<BlogModel> {
	public void index() {
		setAttr("blogPage", BlogModel.dao.paginate(getParaToInt(0, 1), 10));
		render("index.html");
	}
	
	public void add() {
		render("add.html");
	}
	
	@Before(BlogValidator.class)
	public void save() {
		String title = getPara("blog.title");
		System.out.println(title);
		getModel(BlogModel.class).save();
		redirect("");
	}
	
	public void edit() {
		
		setAttr("userInfo", getById2("mysql1","eo_user","uid"));
		//setAttr("userInfo", Db.use("mysql1").findById("eo_user", "uid", 1));
		
		setAttr("blog", BlogModel.dao.findById(getParaToInt()));
		render("edit.html");
	}
	
	@Before(BlogValidator.class)
	public void update() {
		getModel(BlogModel.class).update();
		redirect("/core/blog");
	}
	
	public void delete() {
		BlogModel.dao.deleteById(getParaToInt());
		redirect("/core/blog");
	}
}


